<?php
class Gifts extends MY_Controller
{	
	function __construct() {
        parent::__construct();
    }
    function get_gift_category(){
        // print_r($_POST);exit;
    	$data = $this->user_post_data;
    	if(!empty($data)){

            $sql = "SELECT id,name FROM tiku_gift_categorys where status=1";

            $result = $this->db->query($sql)->result_array();
            
            $data_obj = array(
                'count' => count($result),
                'list' => $result,
             );
            echo json_encode(array("code" => 1, "data" => $data_obj));   // 
    		 // echo $userid;
    	}else{
            echo json_encode(array("code" => 404, "data" => 'not data'));
        }
    }

    /**
     * 获取用户自己的奖品
     * @return [type] [description]
     */
    function get_gift_list(){
        $data = $this->user_post_data;
        if(!empty($data)){
            $limit = !empty($data['limit']) ? $data['limit'] * $this->offset : 0;
            $this->load->model("Gift_model");
            $result = $this->Gift_model->get_user_gift($data['user_token']['user_id'],$limit, $this->offset);
            $result['page_total'] = ceil($result['count'] / $this->offset);
            echo json_encode(array("code" => 1, "data" => $result));   // 
        }else{
            echo json_encode(array("code" => 404, "data" => 'not data'));
        }
    }

    /**
     * 获取所有奖品（自己的、系统的）
     * @return [type] [description]
     */
    function get_all_gift(){
        $data = $this->user_post_data;
        if(!empty($data)){
            $limit = !empty($data['limit']) ? $data['limit'] * $this->offset : 0;
            $this->load->model("Gift_model");
            $result = $this->Gift_model->get_gift_list($limit, $this->offset, array("user_id" => $data['user_token']['user_id']));
            $result['page_total'] = ceil($result['count'] / $this->offset);
            echo json_encode(array("code" => 1, "data" => $result));   // 
        }else{
            echo json_encode(array("code" => 404, "data" => 'not data'));
        }
    }

    function add(){
        $data = $this->user_post_data;
        if(!empty($data)){
            $data['image'] = get_qiniu_url($data['image']);
            $gift = array(
                'category_id' => $data['category_id'], 
                'user_id' => $data['user_token']['user_id'], 
                'name' => $data['name'], 
                'image' => $data['image'], 
                'stars' => $data['stars'], 
                'add_time' => time(), 
            );
            $this->db->insert('tiku_gifts',$gift);
            
            echo json_encode(array("code" => 1, "data" => null));   // 
             // echo $userid;
        }else{
            echo json_encode(array("code" => 404, "data" => 'not data'));
        }
    }

    function get_gift_by_id(){
        $data = $this->user_post_data;
        if(!empty($data)){
            $sql = "SELECT * FROM tiku_gifts where id={$data['gift_id']}";
            $result = $this->db->query($sql)->row_array();

            $child_info = get_field_by_where("user_childrens", "stars,used_stars", "id='{$data['child_id']}'");
            $result['has_stars'] = $child_info['stars'] - $child_info['used_stars'];
            echo json_encode(array("code" => 1, "data" => $result));   // 
             // echo $userid;
        }else{
            echo json_encode(array("code" => 404, "data" => 'not data'));
        }
    }

    function del(){
        $data = $this->user_post_data;
        if(!empty($data)){
            $gift_info = get_field_by_where("gifts", "id,user_id", "id='{$data['gift_id']}'");
            if(!$gift_info){
                echo json_encode(array("code" => 0, "msg" => "奖品不存在"));
                exit;
            }
            if($gift_info['user_id'] != $data['user_token']['user_id'] || $gift_info['user_id'] == 0){
                echo json_encode(array("code" => 0, "msg" => "无法删除改奖品"));
                exit;
            }
            $sql = "DELETE FROM tiku_gifts where id={$data['gift_id']}";
            $result = $this->db->query($sql);
            echo json_encode(array("code" => 1));   // 
             // echo $userid;
        }else{
            echo json_encode(array("code" => 0, "msg" => '缺少数据'));
        }
    }

    function get_user_address(){
        $data = $this->user_post_data;
        $user_info = get_field_by_where("users", "province_code,province_name,city_code,city_name,area_code,area_name,address,addr_name,addr_mobile", "id='{$data['user_token']['user_id']}'");
        echo json_encode(array("code" => 1, "data" => $user_info));
    }

    //领取奖励
    function gift_reward(){
        $data = $this->user_post_data;
        $child_info = get_field_by_where("user_childrens", "stars,used_stars", "id='{$data['child_id']}'");
        $has_stars = $child_info['stars'] - $child_info['used_stars'];  //孩子可用的星星
        $gift_info = get_field_by_where("gifts", "id, name, image, stars", "id='{$data['gift_id']}'");
        if(!$gift_info){
            echo json_encode(array("code" => 0, "msg" => '奖品不存在'));
            exit;
        }
        if($has_stars < $gift_info['stars']){
            echo json_encode(array("code" => 0, "msg" => '您的星星数不够领取该奖品'));
            exit;
        }
        $gift_record_data['user_id'] = $data['user_token']['user_id'];
        $gift_record_data['child_id'] = $data['child_id'];
        $gift_record_data['stars'] = $gift_info['stars'];
        $gift_record_data['gift_id'] = $data['gift_id'];
        $gift_record_data['gift_name'] = $gift_info['name'];
        $gift_record_data['gift_image'] = $gift_info['image'];
        $gift_record_data['addr_mobile'] = $data['addr_mobile'];
        $gift_record_data['address']     = $data['address_name'] . $data['address'];
        $gift_record_data['addr_name']   = $data['addr_name'];
        $gift_record_data['add_time'] = time();
        $this->db->insert("gift_record", $gift_record_data);
        $record_id = $this->db->insert_id();
        if($record_id > 0){
            $this->db->where("id", $data['child_id']);
            $this->db->set("used_stars", "used_stars + " . $gift_info['stars'], false);
            $this->db->update("user_childrens");

            $address['addr_mobile'] = $data['addr_mobile'];
            $address['address']     = $data['address'];
            $address['addr_name']   = $data['addr_name'];
            list($address['province_name'], $address['city_name'], $address['area_name']) = explode(" ", $data['address_name']);
            list($address['province_code'], $address['city_code'], $address['area_code']) = explode(" ", $data['address_code']);
            $this->db->update("users", $address, array("id" => $data['user_token']['user_id']));
            echo json_encode(array("code" => 1, "msg" => '领取成功，请等待家长审核', "record_id" => $record_id));
            exit;
        }
    }

    //获取领取记录
    function get_gift_record_list(){
        $data = $this->user_post_data;
        $this->load->model("Gift_model");
        $limit = !empty($data['limit']) ? $data['limit'] : 0;
        $conditions['child_id'] = $data['user_token']['child_id'];
        $record_list = $this->Gift_model->record_list($limit * $this->offset, $this->offset, $conditions);
        $record_list['total_page'] = ceil($record_list['count'] / $this->offset);
        echo json_encode(array("code" => 1, "data" => $record_list));
    }

    function get_gift_record_by_id(){
        $data = $this->user_post_data;
        $this->load->model("Gift_model");
        $record_info = $this->Gift_model->get_record_info($data['record_id']);
        echo json_encode(array("code" => 1, "data" => $record_info));
    }
}